package com.jtny.nytb.datafilling.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.nytb.datafilling.service.EnergyPriceTempletService;
import com.jtny.nytb.datafilling.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


/**
 * 能源价格填模板填报Controller类
 *
 * @author 智慧能源中心-罗霄林
 * @version 1.0
 * @package com.jtny.nytb.energymonitor.datafilling.controller
 * @fileName EnergyPriceTempletController.java
 * @createTime 2023年09月04日 16:13
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@Api(value = "能源价格填模板填报", tags = "能源价格填模板填报")
@ApiSort(20230901)
public class EnergyPriceTempletController {

    @Autowired
    private EnergyPriceTempletService energyPriceTempletService;
    public final static String OPERATION_SUCCEED = "操作成功";


    @ApiOperation("能源价格模板配置查询")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "energyPriceTempletParamVO", value = "能源价格模板配置查询", required = true, paramType = "query", dataType = "EnergyPriceTempletParamVO")
    @PostMapping("/queryEnergyPriceTempletInfo")
    @ResponseBody
    WebResponse<List<EnergyPriceTempletVO>> queryEnergyPriceTempletInfo(@RequestBody EnergyPriceTempletParamVO energyPriceTempletParamVO) {
        return WebResponse.success(energyPriceTempletService.queryEnergyPriceTempletInfo(energyPriceTempletParamVO));
    }

    @ApiOperation("新增能源价格模板配置")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveEnergyPriceTempletData")
    @ApiImplicitParam(name = "energyPriceTempletParamVO", value = "新增能源价格模板配置", required = true, paramType = "query", dataType = "EnergyPriceTempletParamVO")
    @ResponseBody
    WebResponse<Boolean> saveEnergyPriceTempletData(@RequestBody EnergyPriceTempletParamVO energyPriceTempletParamVO) {
        return WebResponse.success(energyPriceTempletService.saveEnergyPriceTempletData(energyPriceTempletParamVO));
    }

    @ApiOperation("修改能源价格模板配置")
    @ApiOperationSupport(order = 3)
    @PostMapping("/modfiyEnergyPriceTempletData")
    @ApiImplicitParam(name = "energyPriceTempletParamVO", value = "修改能源价格模板配置", required = true, paramType = "query", dataType = "EnergyPriceTempletParamVO")
    @ResponseBody
    WebResponse<?> modfiyEnergyPriceTempletData(@RequestBody EnergyPriceTempletParamVO energyPriceTempletParamVO) {
        energyPriceTempletService.modfiyEnergyPriceTempletData(energyPriceTempletParamVO);
        return WebResponse.success(OPERATION_SUCCEED);
    }

    @ApiOperation("删除能源价格模板配置")
    @ApiOperationSupport(order = 4)
    @GetMapping("/delEnergyPriceTempletData")
    @ApiImplicitParam(name = "id", value = "数据id", required = true, paramType = "query", dataType = "String")
    @ResponseBody
    WebResponse<Boolean> delEnergyPriceTempletData(@RequestParam("id") String id) {
        return WebResponse.success(energyPriceTempletService.delEnergyPriceTempletData(id));
    }

    @ApiOperation("制度下拉查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "typeId", value = "能源类型", required = true, paramType = "query", dataType = "String")
    @GetMapping("/queryInstitutionAndElericNameInfo")
    @ResponseBody
    WebResponse<Map<String, List<String>>> queryInstitutionAndElericNameInfo(String typeId) {
        return WebResponse.success(energyPriceTempletService.queryInstitution(typeId));
    }

    @ApiOperation("查询等级下拉")
    @GetMapping("/institution/queryLevel")
    public WebResponse<List<String>> queryLevel(EnergyPriceTempletParamVO energyModelVO) {
        return WebResponse.success(energyPriceTempletService.queryLevel(energyModelVO));
    }


    @ApiOperation("查询类型下拉")
    @GetMapping("/institution/queryElType")
    public WebResponse<List<String>> queryElType(String typeId) {
        return WebResponse.success(energyPriceTempletService.queryElType(typeId));
    }

    @ApiOperation("根据制度和用能查询名称（分组名称）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "typeId", value = "能源类型", required = true, paramType = "query", dataType = "String")
    @GetMapping("/queryNameInfo")
    @ResponseBody
    WebResponse<NameOrGroupContentVO> queryNameOrGroupGInfo(String typeId, String Institution) {
        return WebResponse.success(energyPriceTempletService.queryName(typeId, Institution));
    }

    @ApiOperation("导入分类")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParam(name = "type", value = "能源类型", required = true, paramType = "query", dataType = "String")
    @PostMapping("/importEnergyPriceTempletData")
    @ResponseBody
    WebResponse<Boolean> importEnergyPriceTempletData(@RequestPart("file") MultipartFile file, @RequestParam("type") String type) throws IOException, InvocationTargetException, IllegalAccessException, InstantiationException {
        return WebResponse.success(energyPriceTempletService.importEnergyPriceTempletData(file, type));
    }

    @ApiOperation("下载能源消耗模板")
    @ApiOperationSupport(order = 7)
    @GetMapping("/downloadEnergyTemplate")
    @ResponseBody
    public WebResponse<?> downloadEnergyTemplate(HttpServletResponse response, @RequestParam("typeId") String typeId) {
        try {
            energyPriceTempletService.downloadPriceTemplate(response, typeId);
            return WebResponse.success("下载成功");
        } catch (ServiceException e) {
            return WebResponse.error("下載失败");
        }
    }

    @ApiOperation("能源模板配置查询")
    @ApiOperationSupport(order = 8)
    @PostMapping("/getEnergyPriceTempletInfo")
    @ResponseBody
    WebResponse<List<EnergyModelVO>> getEnergyPriceTempletInfo(@RequestBody EnergyModelPriceParamVO energyModelPriceParamVO) {
        return WebResponse.success(energyPriceTempletService.getEnergyPriceTempletInfo(energyModelPriceParamVO));
    }

    @ApiOperation("能源模板价格清空")
    @ApiOperationSupport(order = 9)
    @GetMapping("/deleteEnergyTemplate/{id}")
    @ResponseBody
    WebResponse<?> deleteEnergyTemplate(@PathVariable("id") Long id) {
        energyPriceTempletService.deleteEnergyTemplate(id);
        return WebResponse.success(OPERATION_SUCCEED);
    }

    @ApiOperation("能源模板详情")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getEnergyTemplateInfo/{id}")
    @ResponseBody
    WebResponse<List<EnergyModelVO>> getEnergyTemplateInfo(@PathVariable("id") Long id) {
        List<EnergyModelVO> energyModelVOList = energyPriceTempletService.getEnergyTemplateInfo(id);
        return WebResponse.success(energyModelVOList);
    }

    @ApiOperation("能源模板删除")
    @ApiOperationSupport(order =11)
    @GetMapping("/deleteEnergyTemplateById/{id}")
    @ResponseBody
    WebResponse<?> deleteEnergyTemplateById(@PathVariable("id") Long id) {
        energyPriceTempletService.deleteEnergyTemplateById(id);
        return WebResponse.success(OPERATION_SUCCEED);
    }
}
